/**
 * @(#)IndexAjaxController.java, 2018/2/9.
 * <p/>
 * Copyright 2018 Kerr Wang, Inc. All rights reserved.
 * KERRWANG PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 */
package org.wangzx.exercise.controller.xhr;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.wangzx.exercise.enums.LoginType;
import org.wangzx.exercise.meta.vo.ItemVO;
import org.wangzx.exercise.meta.vo.UserVO;
import org.wangzx.exercise.meta.vo.common.AjaxResult;
import org.wangzx.exercise.service.basic.ItemService;
import org.wangzx.exercise.service.core.UserCoreService;
import org.wangzx.exercise.utils.LoginUtils;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * @author hzwangzuxiang(ahscwzx@163.com)
 */
@Controller
@RequestMapping("/")
public class IndexAjaxController extends BaseAjaxController {
    static private final Logger logger = LoggerFactory.getLogger(IndexAjaxController.class);

    @Autowired
    private ItemService itemService;

    @Autowired
    private UserCoreService userCoreService;

    @RequestMapping("/getAllItems.json")
    @ResponseBody
    public AjaxResult getAllItem(HttpServletRequest request) {
        logger.info("[op:index] getAllItems");
        UserVO userVO = LoginUtils.loginCheck(request);
        List<ItemVO> voList = itemService.getAllItemVO(userVO);
        return initSuccessResult(voList);
    }

    @RequestMapping("/getNotBuyItems.json")
    @ResponseBody
    public AjaxResult getNotBuyItems(HttpServletRequest request) {
        logger.info("[op:index] getNotBuyItems");
        List<ItemVO> voList = new ArrayList<>();
        UserVO userVO = LoginUtils.loginCheck(request);
        if(userVO.getLoginType() != LoginType.BUYER.getValue()) {
            return initSuccessResult(voList);
        }
        long userId = userCoreService.getByName(userVO.getUserName()).getId();
        voList = itemService.getNotBuyItems(userId);
        return initSuccessResult(voList);
    }
}
